import threading
from django.core.mail import send_mail
from django.conf import settings

# 异步发送邮件
class EmailSender(threading.Thread):
    '''
    多线程异步发送邮件类
    '''
    def __init__(self, subject, message, email_to, html_message=None ,fail_silently=False):
        self.subject = subject
        self.message = message
        self.email_to = email_to
        self.fail_silently = fail_silently
        self.html_message = html_message
        super(EmailSender, self).__init__()
    
    def run(self):
        if self.email_to != '':
            send_mail(self.subject, self.message, settings.EMAIL_FROM, [self.email_to], 
                            fail_silently=self.fail_silently, html_message=self.html_message)
